import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

public class WordPageFooterExample {
	public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException {
		// 创建一个新的Word文档
		XWPFDocument document = new XWPFDocument();

		// 创建一个段落并添加一些内容
		XWPFParagraph paragraph = document.createParagraph();

        // 创建第一个文本片段 - 宋体
        XWPFRun run1 = paragraph.createRun();
        run1.setText("这是宋体文字 ");
        run1.setFontFamily("宋体");
        run1.setFontSize(12);
        
        // 创建第二个文本片段 - 黑体
        XWPFRun run2 = paragraph.createRun();
        run2.setText("这是黑体文字 ");
        run2.setFontFamily("黑体");
        run2.setFontSize(14);
        run2.setBold(true);
        
        // 创建第三个文本片段 - 楷体
        XWPFRun run3 = paragraph.createRun();
        run3.setText("这是楷体文字");
        run3.setFontFamily("楷体");
        run3.setFontSize(16);
        run3.setItalic(true);
        run3.setColor("FF0000"); // 红色

		XWPFRun run = paragraph.createRun();
//		run.setText("这是文档的正文内容,生僻字𬱖。第1页");
//		setXWPFRunStyle(run, "宋体", 9);
		run.setBold(true);
		run.setItalic(true);
		run.setColor("FF0000");
        // 设置亚洲字体(主要影响中文)
        run.setFontFamily("宋体", XWPFRun.FontCharRange.eastAsia);
        run.setFontFamily("方正宋体-人口信息-P2", XWPFRun.FontCharRange.ascii);
        // 设置西方字体(主要影响英文)
        run.setFontFamily("Times New Roman", XWPFRun.FontCharRange.cs);
        
        // 设置字体大小
        run.setFontSize(12);
        
        // 添加混合文本
        run.setText("这是一个示例文本Sample text with 中英文混合Chinese and English.这是文档的正文内容,生僻字𬱖。第1页");

		paragraph = document.createParagraph();
		run = paragraph.createRun();
		run.addBreak(BreakType.PAGE);
		run.setText("这是文档的正文内容,生僻字𬱖。第2页");
		setXWPFRunStyle(run, "楷体", 12);

		paragraph = document.createParagraph();
		run = paragraph.createRun();
		run.addBreak(BreakType.PAGE);
		run.setText("这是文档的正文内容。第3页");
		setXWPFRunStyle(run, "黑体", 14);

		paragraph = document.createParagraph();
		run = paragraph.createRun();
		run.addBreak(BreakType.PAGE);
		run.setText("这是文档的正文内容。第4页");
		setXWPFRunStyle(run, "仿宋", 16);

		paragraph = document.createParagraph();
		run = paragraph.createRun();
		run.addBreak(BreakType.PAGE);
		run.setText("这是文档的正文内容。第5页");
		setXWPFRunStyle(run, "微软雅黑", 20);

		createFooter(document);

		// 将文档写入文件
		try {
			FileOutputStream out = new FileOutputStream("WordPageFooterExample7.docx");
			document.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
	}

	// https://blog.yanqingshan.com/50.html
	// https://segmentfault.com/q/1010000023477908
	/**
	 * 创建页脚
	 *
	 * @param document
	 *            doc对象
	 * @throws IOException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public static void createFooter(XWPFDocument document)
			throws IOException, NoSuchFieldException, IllegalAccessException {
		Field _settings = XWPFDocument.class.getDeclaredField("settings");
		_settings.setAccessible(true);
		XWPFSettings xwpfsettings = (XWPFSettings) _settings.get(document);
		Field _ctSettings = XWPFSettings.class.getDeclaredField("ctSettings");
		_ctSettings.setAccessible(true);
		CTSettings ctSettings = (CTSettings) _ctSettings.get(xwpfsettings);
		ctSettings.addNewEvenAndOddHeaders();

		XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
		// 奇数
		XWPFParagraph paragraph = footer.createParagraph();
		paragraph.setAlignment(ParagraphAlignment.RIGHT);
		XWPFRun run;
		run = paragraph.createRun();
		run.setText("— ");
		CTFldChar fldChar = run.getCTR().addNewFldChar();
//		fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
		fldChar.setFldCharType(STFldCharType.BEGIN);
		setXWPFRunStyle(run, "宋体", 14);
		CTText ctText = run.getCTR().addNewInstrText();
		ctText.setStringValue("PAGE  \\* MERGEFORMAT");
//		ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
		ctText.setSpace(SpaceAttribute.Space.PRESERVE);
		setXWPFRunStyle(run, "宋体", 14);
		fldChar = run.getCTR().addNewFldChar();
//		fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
		fldChar.setFldCharType(STFldCharType.END);
		run.setText(" —");

		// 偶数
		XWPFParagraph paragraph1 = document.createFooter(HeaderFooterType.EVEN).createParagraph();
		paragraph1.setAlignment(ParagraphAlignment.LEFT);
		XWPFRun run1;
		run1 = paragraph1.createRun();
		run1.setText("— ");
		CTFldChar fldChar1 = run1.getCTR().addNewFldChar();
		fldChar1.setFldCharType(STFldCharType.Enum.forString("begin"));
		setXWPFRunStyle(run1, "宋体", 14);
		CTText ctText1 = run1.getCTR().addNewInstrText();
		ctText1.setStringValue("PAGE  \\* MERGEFORMAT");
		ctText1.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
		setXWPFRunStyle(run1, "宋体", 14);
		fldChar1 = run1.getCTR().addNewFldChar();
		fldChar1.setFldCharType(STFldCharType.Enum.forString("end"));
		run1.setText(" —");
	}

	/**
	 * 设置页脚的字体样式
	 *
	 * @param r1
	 *            段落元素
	 * @param font
	 *            字体
	 * @param fontSize
	 *            字体大小
	 */
	private static void setXWPFRunStyle(XWPFRun r1, String font, int fontSize) {
		r1.setFontSize(fontSize);
		CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr();
		CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
		fonts.setAscii(font);
		fonts.setEastAsia(font);
		fonts.setHAnsi(font);
	}
}